/*
 * AddClient.java
 *
 * Created on Jan 15, 2011, 3:07:43 PM
 */
package edu.rit.varapp.graphical;

import edu.rit.varapp.datalayer.Customer;
import edu.rit.varapp.datalayer.CustomerOps;
import edu.rit.varapp.utilities.ExceptionUtils;
import java.util.Arrays;
import java.awt.Dimension;
import java.sql.SQLException;
import javax.swing.JOptionPane;

import static edu.rit.varapp.utilities.StringUtils.emptyString;
import static edu.rit.varapp.utilities.ValidationUtils.*;
/**
 *
 * @author Eric Kisner
 * @author Michael Klein
 */
public class AddClient extends AbstractView {

    private static final int VIEW_WIDTH = 410;
    private static final int VIEW_HEIGHT = 477;
    private final CustomerOps cOps = new CustomerOps();

    /** Creates new form AddClient */
    public AddClient() {
        super( new Dimension( VIEW_WIDTH, VIEW_HEIGHT ) );
        initComponents();
    }
    
    /**
     * Validates any input for the text fields of this GUI.
     * @return True if all fields are valid, else false.
     */
    private boolean validateInput() {
        String[] methodNames = {
            "VarCharLength", "Title", "Name", "Email", "Char", "State"
        };
        String[] messages = {
            "Company Name", "Title (ex. Mr/Ms)", "Contact Name (ex. John Smith)",
            "Email Address (test@test.com)", "City", "State (ex. NY)"
        };
        String[] values = {
            companyNameField.getText(), titleField.getText(), contactNameField.getText(),
            emailField.getText(), cityField.getText(), stateField.getText()
        };

        String errorMessage = runValidateMethodList( Arrays.asList( methodNames ),
                                                     Arrays.asList( messages ),
                                                     Arrays.asList( values ) );
        if( errorMessage.equals( emptyString() ) ) {
            return true;
        } else {
            JOptionPane.showMessageDialog( rootPane, errorMessage );
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        usernameLabel4 = new javax.swing.JLabel();
        titleField = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        contactNameField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        companyNameField = new javax.swing.JTextField();
        serverLabel = new javax.swing.JLabel();
        address1Field = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        address2Field = new javax.swing.JTextField();
        usernameLabel1 = new javax.swing.JLabel();
        cityField = new javax.swing.JTextField();
        usernameLabel2 = new javax.swing.JLabel();
        stateField = new javax.swing.JTextField();
        usernameLabel3 = new javax.swing.JLabel();
        zipcodeField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        resetFieldsButton = new javax.swing.JButton();
        addCustomerButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add New Client");
        setMinimumSize(new java.awt.Dimension(410, 477));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Add New Client");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(130, 20, 130, 30);

        jPanel1.setLayout(new java.awt.GridLayout(9, 2, 5, 5));

        usernameLabel4.setText("Title:");
        jPanel1.add(usernameLabel4);
        jPanel1.add(titleField);

        usernameLabel.setText("Name:");
        jPanel1.add(usernameLabel);
        jPanel1.add(contactNameField);

        passwordLabel.setText("Company:");
        jPanel1.add(passwordLabel);
        jPanel1.add(companyNameField);

        serverLabel.setText("Address 1:");
        jPanel1.add(serverLabel);
        jPanel1.add(address1Field);

        jLabel2.setText("Address 2:");
        jPanel1.add(jLabel2);
        jPanel1.add(address2Field);

        usernameLabel1.setText("City:");
        jPanel1.add(usernameLabel1);
        jPanel1.add(cityField);

        usernameLabel2.setText("State:");
        jPanel1.add(usernameLabel2);
        jPanel1.add(stateField);

        usernameLabel3.setText("Zipcode:");
        jPanel1.add(usernameLabel3);
        jPanel1.add(zipcodeField);

        jLabel3.setText("Email:");
        jPanel1.add(jLabel3);
        jPanel1.add(emailField);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(35, 60, 310, 300);

        resetFieldsButton.setText("Reset Fields");
        resetFieldsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetFieldsButtonActionPerformed(evt);
            }
        });
        getContentPane().add(resetFieldsButton);
        resetFieldsButton.setBounds(190, 370, 149, 31);

        addCustomerButton.setText("Add Customer");
        addCustomerButton.setMaximumSize(new java.awt.Dimension(134, 28));
        addCustomerButton.setMinimumSize(new java.awt.Dimension(134, 28));
        addCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCustomerButtonActionPerformed(evt);
            }
        });
        getContentPane().add(addCustomerButton);
        addCustomerButton.setBounds(40, 370, 149, 31);

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        getContentPane().add(backButton);
        backButton.setBounds(120, 400, 149, 31);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCustomerButtonActionPerformed
        if (validateInput()){
            String contactname = contactNameField.getText();
            String companyname = companyNameField.getText();
            String title = titleField.getText();
            String email = emailField.getText();
            String address1 = address1Field.getText();
            String address2 = address2Field.getText();
            String city = cityField.getText();
            String state = stateField.getText();
            String zipcode = zipcodeField.getText();

            /**
             * Since the VAR will not have the EntityId for the customer during insertion, we need to
             * retrieve an unallocated EntityId to give to the new Customer.
             */
            int entityId = 1;
            try {
                entityId = cOps.newEntityId();
            } catch( SQLException ignore ) {
                //We can try assigning 1 as an entityId to the customer, and then catching any errors that may occur
                //if there is a duplicate key. Otherwise, it would be safe to have a customer have '1' as their
                //temporary entityId.
            }

            // create new customer object
            Customer customer = new Customer( entityId, title, contactname, companyname, address1,
                                              address2, city, state, zipcode, email );

            //insert the object and catch errors
            try {
                if( cOps.insertCustomer( customer ) ) {
                    JOptionPane.showMessageDialog( rootPane, "Successfully added customer." );
                } else {
                    JOptionPane.showMessageDialog( rootPane, "The customer has not been added. Check that you have not "
                                                             + "already added the person." );
                }
                new AdminOperations().setVisible( true );
                close( false );
            } catch( SQLException sqle ) {
                ExceptionUtils.handleException( this, sqle, "There was a problem adding customer "
                                                            + customer.getContactName() + "'s information. Please check "
                                                            + "the log files for more information before trying again.",
                                                this.getClass() );
                return;
            }
        }
    }//GEN-LAST:event_addCustomerButtonActionPerformed

    private void resetFieldsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetFieldsButtonActionPerformed
        contactNameField.setText( "" );
        companyNameField.setText( "" );
        titleField.setText( "" );
        emailField.setText( "" );
        address1Field.setText( "" );
        address2Field.setText( "" );
        cityField.setText( "" );
        stateField.setText( "" );
        zipcodeField.setText( "" );
    }//GEN-LAST:event_resetFieldsButtonActionPerformed


    private void backButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_backButtonActionPerformed
    {//GEN-HEADEREND:event_backButtonActionPerformed
        new AdminOperations().setVisible( true );
        close( false );
    }//GEN-LAST:event_backButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCustomerButton;
    private javax.swing.JTextField address1Field;
    private javax.swing.JTextField address2Field;
    private javax.swing.JButton backButton;
    private javax.swing.JTextField cityField;
    private javax.swing.JTextField companyNameField;
    private javax.swing.JTextField contactNameField;
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JButton resetFieldsButton;
    private javax.swing.JLabel serverLabel;
    private javax.swing.JTextField stateField;
    private javax.swing.JTextField titleField;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JLabel usernameLabel1;
    private javax.swing.JLabel usernameLabel2;
    private javax.swing.JLabel usernameLabel3;
    private javax.swing.JLabel usernameLabel4;
    private javax.swing.JTextField zipcodeField;
    // End of variables declaration//GEN-END:variables
}
